package com.dc.adaptor.transport.addressconvertor;

import com.dc.adaptor.transport.TransportMeta;
import com.dc.adaptor.transport.wmq.WmqMeta;

public class MQAddressConvertor implements AddressConvertor<TransportMeta> {

	@Override
	public String getAddress(TransportMeta tansMeta) {
		if (tansMeta == null) {
			throw new RuntimeException("involid parameter WmqMeta : "
					+ " null ");
		}
		WmqMeta meta = (WmqMeta) tansMeta;
		StringBuffer address = new StringBuffer("");
		address.append("mq").append(AddressConvertor.SEPARATOR).append(
				processNullToString(meta.getIp())).append(
				AddressConvertor.SEPARATOR).append(
				processNullToString(meta.getPort())).append(
				AddressConvertor.SEPARATOR).append(
				processNullToString(meta.getQmanager())).append(
				AddressConvertor.SEPARATOR).append(
				processNullToString(meta.getQueue())).append(
				AddressConvertor.SEPARATOR).append(
				processNullToString(meta.getChannel())).append(
				AddressConvertor.SEPARATOR).append(
				processNullToString(meta.getCcsid())).append(
				AddressConvertor.SEPARATOR).append(
				processNullToString(meta.getMessageEncoding())).append(
				AddressConvertor.SEPARATOR).append(meta.getProcessInterval())
				.append(AddressConvertor.SEPARATOR).append(
						meta.getIntervalOnError()).append(
						AddressConvertor.SEPARATOR).append(
						meta.getIntervalOnNotMessage()).append(
						AddressConvertor.SEPARATOR).append(meta.isOneoff())
				.append(AddressConvertor.SEPARATOR).append(meta.isCompress());
		return address.toString();
	}

	@Override
	public TransportMeta getMeta(String address) {
		final String[] addresses = address.split(AddressConvertor.SEPARATOR);
		if (addresses.length != 13) {
			throw new RuntimeException("invalid parameter for the charaters: '"
					+ address + "'");
		}
		WmqMeta meta = new WmqMeta() {

			@Override
			public String getCcsid() {
				return processBlankString(addresses[6]);
			}

			@Override
			public String getChannel() {
				return processBlankString(addresses[5]);
			}

			@Override
			public long getIntervalOnError() {
				return Long.parseLong(addresses[9]);
			}

			@Override
			public long getIntervalOnNotMessage() {
				return Long.parseLong(addresses[10]);
			}

			@Override
			public String getIp() {
				return processBlankString(addresses[1]);
			}

			@Override
			public String getMessageEncoding() {
				return processBlankString(addresses[7]);
			}

			@Override
			public String getPort() {
				return processBlankString(addresses[2]);
			}

			@Override
			public long getProcessInterval() {
				return Long.parseLong(addresses[8]);
			}

			@Override
			public String getQmanager() {
				return processBlankString(addresses[3]);
			}

			@Override
			public String getQueue() {
				return processBlankString(addresses[4]);
			}

			@Override
			public boolean isOneoff() {
				if ("Y".equals(addresses[11])) {
					return true;
				}
				return false;
			}

			@Override
			public boolean isCompress() {
				if ("Y".equals(addresses[12])) {
					return true;
				}
				return false;
			}

		};
		return meta;
	}

	private String processNullToString(String orinString) {
		if (orinString == null) {
			return "";
		}
		return orinString;
	}

	private String processBlankString(String orinString) {
		if (orinString.length() == 0) {
			return null;
		}
		return orinString;
	}
}
